<template>
  <div class="layout">
    <div style="display: flex; justify-content: center">
      <Breadcrumb class="breadcrumb" :crumbs="crumbs"></Breadcrumb>
    </div>
    <div style="display: flex; justify-content: center;">
      <div id="selectTypeDiv">
        <div class="typeBtn" style="background-color: #D00000; color: #FFFFFF">全部政策</div>
        <div class="typeBtn" @click="hrefTo('policyRecommend')">智能推荐</div>
      </div>
    </div>
    <SelectBox @changeOption="changeOption" style="margin-bottom: 30px" type="other"></SelectBox>
    <div id="selectBar">
      <div class="leftDiv">
        <div class="selectOptionDiv" @click="changeShowDateOrder">
          <div style="margin-right: 5px">发布时间</div>
          <img v-show="orderShowDate %3 == 0" src="@/assets/image/policyList/orderIcon.png" />
          <img v-show="orderShowDate %3 == 1" src="@/assets/image/policyList/orderDescIcon.png" />
          <img v-show="orderShowDate %3 == 2" src="@/assets/image/policyList/orderAscIcon.png" />
        </div>
        <span class="divide">|</span>
        <div class="selectOptionDiv" @click="changePvOrder">
          <div style="margin-right: 5px">浏览量</div>
          <img v-show="orderPv %3 == 0" src="@/assets/image/policyList/orderIcon.png" />
          <img v-show="orderPv %3 == 1" src="@/assets/image/policyList/orderDescIcon.png" />
          <img v-show="orderPv %3 == 2" src="@/assets/image/policyList/orderAscIcon.png" />
        </div>
        <span class="divide">|</span>
        <el-checkbox v-model.trim="showCanSend" @change="changeCanSend">仅显示可申报政策</el-checkbox>
      </div>
      <div class="rightDiv">
        <div @click="toPrevPage()" :class="pageNum <= 1 ? 'pageBtn  pageCantBtn' : 'pageBtn'"><</div>
        <div @click="toNextPage()" :class="isLastPage() ? 'pageBtn  pageCantBtn' : 'pageBtn'">></div>
      </div>
    </div>
    <div id="policyList" v-loading="policyLoading">
      <PolicyFrame v-for="policy in policys" :policy="policy" :key="policy.id"></PolicyFrame>
    </div>
    <div style="width: 1200px; margin: 0 auto">
      <div id="policyPagination" v-if="total > 0">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          background
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import SelectBox from './components/selectBox'
import Breadcrumb from '@/components/breadcrumb'
import PolicyFrame from '@/components/policyFrame'

import { getDoingPolicys } from "@/common/api/policy.js";

export default {
  name: 'policyList',
  components: { SelectBox, Breadcrumb, PolicyFrame },
  data() {
    return {
      pageNum: 1,
      pageSize: 10,
      total: 0,
      type: null,
      tag: null,
      orderShowDate: 1, //发布时间
      orderPv: 0, //浏览量
      showCanSend: false,
      policyLoading:false,
      policys: [],
      crumbs: [
        {
          label: '政策服务',
          value: 'policy',
        },
        {
          label: '政策清单',
          value: 'policyList',
          choose: true,
        },
      ],
    }
  },
  methods: {
    /**
     * 判断是否为最后一页
     */
    isLastPage: function () {
      let ns = this
      return ns.pageNum == Math.ceil(ns.total / ns.pageSize)
    },
    /**
     * 上一页
     */
    toPrevPage: function () {
      let ns = this
      if (ns.pageNum <= 1) {
        return
      }
      ns.pageNum--
      ns.getPolicys()
    },
    /**
     * 下一页
     */
    toNextPage: function () {
      let ns = this
      if (ns.isLastPage()) {
        return
      }
      ns.pageNum++
      ns.getPolicys()
    },
    /**
     * 跳转到政策详情页面
     */
    toPolicyDetail: function (policy) {
      let ns = this
      ns.$router.push({ path: 'policyDetail', query: { id: policy.id } })
    },
    /**
     * 跳转页面
     * @param url 要跳转的页面
     */
    hrefTo: function (url) {
      let ns = this
      ns.$router.push({ name: url })
    },
    /**
     * 格式化标签
     * @param label 标签名
     * @return {string|*} 标签名对象
     */
    formatLabel: function (label) {
      return formatLabel(label)
    },
    /**
     * 改变页面大小事件
     */
    handleSizeChange: function (e) {
      let ns = this
      ns.pageSize = e
      ns.getPolicys()
    },
    /**
     * 换页事件
     */
    handleCurrentChange: function (e) {
      let ns = this
      ns.pageNum = e
      ns.getPolicys()
    },
    /**
     * 修改筛选条件事件
     * @param e 修改后的筛选条件对象
     */
    changeOption: function (e) {
      let ns = this
      ns.type = e.type.id != undefined ? e.type.id : null
      ns.tag = e.tag.id != undefined ? e.tag.id : null
      ns.getPolicys()
    },
    /**
     * 修改发布时间排序
     */
    changeShowDateOrder: function () {
      let ns = this
      ns.orderShowDate++
      ns.orderPv = 0
      ns.getPolicys()
    },
    /**
     * 修改浏览量排序
     */
    changePvOrder: function () {
      let ns = this
      ns.orderPv++
      ns.orderShowDate = 0
      ns.getPolicys()
    },
    /**
     * 修改仅显示可申报政策选项
     */
    changeCanSend: function (e) {
      let ns = this
      ns.pageNum = 1; //恢复页码的初始值
      ns.showCanSend = e
      ns.getPolicys()
    },
    /**
     * 获取政策信息
     */
    async getPolicys() {
      let ns = this
      let sortType = null
      let sort = null
      ns.policyLoading = true;
      if (ns.orderShowDate > 0) {
        sortType = 1
        sort = ns.orderShowDate % 3
      }
      if (ns.orderPv > 0) {
        sortType = 2
        sort = ns.orderPv % 3
      }
      if (sort == 2) {
        sort = 1
      } else if (sort == 1) {
        sort = 2
      }
      let data = {
        pageNum: ns.pageNum,
        pageSize: ns.pageSize,
        type: ns.type,
        tag: ns.tag,
        status: ns.showCanSend ? 1 : null,
        sortType: sortType,
        sort: sort,
        entrance: 1,
      }
      let res = await getDoingPolicys(data)
      ns.policys = res.rows
      console.log(ns.policys)
      ns.total = res.total
      for (let i = 0; i < ns.policys.length; i++) {
        ns.policys[i].description = ns.policys[i].description.replace(
          /<[^<>]+>/g,
          ''
        )
      }
      ns.policyLoading = false;
    },
  },
  mounted() {
  	let ns = this
  	ns.getPolicys()
  },
  activated() {
    let ns = this
    ns.getPolicys()
    // this.$refs.headerBars.setCompanyInfo()
    // this.$refs.headerBars.isLogin()
  },
}
</script>

<style scoped>
.breadcrumb {
  width: 1200px;
  margin: 20px 0;
}

#selectTypeDiv {
  width: 1200px;
  display: flex;
  border-bottom: 3px solid #d00000;
  padding: 0 20px;
  margin-bottom: 30px;
}

#selectTypeDiv div {
  padding: 5px 20px;
}

#selectTypeDiv .typeBtn:hover {
  background-color: #d00000;
  color: #ffffff;
  cursor: pointer;
  transition: all 0.2s ease 0s;
}

#selectBar {
  height: 50px;
  background-color: #f4f4f4;
  width: 1200px;
  margin: 0 auto;
  border: 1px solid #e1e1e1;
  margin-bottom: 30px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  font-size: 14px;
  color: #606266;
}

#selectBar .leftDiv {
  display: flex;
  align-items: center;
  justify-content: left;
}

#selectBar .leftDiv .selectOptionDiv {
  display: flex;
  align-items: center;
}

#selectBar .leftDiv .selectOptionDiv:hover {
  cursor: pointer;
}

#selectBar .leftDiv .divide {
  margin: 0 20px;
}

#selectBar .rightDiv {
  display: flex;
}

#selectBar .rightDiv .pageBtn {
  background-color: #ffffff;
  margin-left: 10px;
  border: 1px solid #eeeeee;
  width: 40px;
  padding: 8px 0;
  font-size: 20px;
  border-radius: 3px;
  color: #666666;
}

#selectBar .rightDiv .pageCantBtn {
  color: #d4d4d7;
}

#selectBar .rightDiv .pageBtn:hover {
  cursor: pointer;
}

#policyList {
  display: flex;
  width: 1200px;
  flex-wrap: wrap;
  justify-content: space-between;
  margin: 0 auto;
}

#policyPagination {
  text-align: right;
  margin-bottom: 30px;
}

.layout {
  min-height: 100vh;
  position: relative;
  padding-bottom: 150px;
  box-sizing: border-box;
}

.footer {
  position: absolute;
  bottom: 0;
  width: 100%;
}
</style>
